'use strict'

import {app, BrowserWindow, globalShortcut, ipcMain} from 'electron'

const path = require("path")
const os = require("os");
const setShopWin = require('@/util/setShopWin');
const secretUtil = require('@/util/secretUtil');
const { enable } = require('@/util/enable');
const file = require('@/util/file');

const isWin7 = os.release().startsWith('6.1')
// win7 下关闭硬件加速
if (isWin7) app.disableHardwareAcceleration()
const isDevelopment = process.env.NODE_ENV !== "production"
const domain = process.env.VUE_APP_WEB_DOMAIN
let win, secret = '';

const gotTheLock = app.requestSingleInstanceLock()
if (!gotTheLock) {
  app.quit()
} else {
  app.on("second-instance", (event, commandLine, workingDirectory) => {
    if (win) {
      if (win.isMinimized()) win.restore()
      win.focus()
      win.show()
    }
  })

  function createWindow() {
    win = new BrowserWindow({
      minWidth: 500,//设置窗口宽度
      minHeight: 500,//设置窗口高度
      // transparent: true,
      title: '结账',
      resizable: false,
      maximizable: false,
      frame: false,
      show: false,
      icon: path.join(__dirname, 'logo.ico'),
      webPreferences: {
        partition: String(+new Date()),
        plugins: true, //是否支持插件
        nodeIntegration: true,
        contextIsolation: false,
        webViewTag: true,
        webSecurity: false,
        enableRemoteModule: true
      }
    })

    win.setMenu(null);
    // 在开发环境和生产环境均可通过快捷键打开devTools
    // win.webContents.openDevTools({mode:'detach'});

    globalShortcut.register('CommandOrControl+Alt+c', function () {
      win.webContents.openDevTools({mode:'detach'});
    })

    if (process.env.ENV == 'development') {
      win.loadURL(`${domain}/index?secret=${secret}`, { extraHeaders: "pragma: no-cache\n" });
    } else {
      win.loadURL(`file://${path.join(__dirname, 'index.html')}#/index?secret=${secret}`);
    }

    enable(win);
    win.once('closed', () => {
      win = null
    })
    win.once('ready-to-show', () => {
      win.show();
    });
    win.on('blur', () => {
      if (win.isMinimized()) win.restore()
    });

    win.on('minimize', (event) => {
      event.preventDefault();
      if (win.isMinimized()) win.restore()
      win.show()
    });
    ipcMain.on('set-main-size', (event, size) => {
      win.setSize(size.width, size.height)
      win.center()
    })
    ipcMain.on('quit', () => {
      if(win) win.close()
      app.exit();
    })
  }
  app.on("window-all-closed", () => {
    if (process.platform !== "darwin") {
      app.quit()
    }
  })

  app.on("activate", () => {
    if (win === null) {
      createWindow()
    }
  })
  app.on("ready", async () => {
    const fun = (shopId) =>{
      secret = secretUtil.get(shopId)
      createWindow()
    }
    file.configRead().then((shopId) => {
      fun(shopId)
    }).catch(() => {
      setShopWin.createSetShopWin((shopId)=>{
        fun(shopId)
      })
    })
  })
}
if (isDevelopment) {
  if (process.platform === "win32") {
    process.on("message", (data) => {
      if (data === "graceful-exit") {
        app.quit()
      }
    })
  } else {
    process.on("SIGTERM", () => {
      app.quit()
    })
  }
}
